/*
       参数解释：
       title	标题
       url		请求的url
       id		需要操作的数据id
       w		弹出层宽度（缺省调默认值）
       h		弹出层高度（缺省调默认值）
   */
$(function () {
    Date.prototype.format = function (fmt) {
        var o = {
            "M+": this.getMonth() + 1,                 //月份
            "d+": this.getDate(),                    //日
            "h+": this.getHours(),                   //小时
            "m+": this.getMinutes(),                 //分
            "s+": this.getSeconds(),                 //秒
            "q+": Math.floor((this.getMonth() + 3) / 3), //季度
            "S": this.getMilliseconds()             //毫秒
        };
        if (/(y+)/.test(fmt)) {
            fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
        }
        for (var k in o) {
            if (new RegExp("(" + k + ")").test(fmt)) {
                fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
            }
        }
        return fmt;
    }

    $("#today").click(function () {
        var start = new Date(new Date().getTime()).format("yyyy-MM-dd");
        var end = new Date(new Date().getTime()).format("yyyy-MM-dd");
        $("#startTime").val(start);
        $("#endTime").val(end);
    });
    $("#week").click(function () {
        var start = new Date(new Date().getTime() - 6 * 24 * 60 * 60 * 1000).format("yyyy-MM-dd");
        var end = new Date(new Date().getTime()).format("yyyy-MM-dd");
        $("#startTime").val(start);
        $("#endTime").val(end);
    });
    $("#month").click(function () {
        var start = new Date(new Date().setDate(1)).format("yyyy-MM-dd");
        var end = new Date(new Date().getTime()).format("yyyy-MM-dd");
        $("#startTime").val(start);
        $("#endTime").val(end);
    });


    var $table = $("#tableMoreTimesFeal");
    var _table = $table.dataTable($.extend(true, {}, CONSTANT.DATA_TABLES.DEFAULT_OPTION, {
        ajax: function (data, callback, settings) {
            //封装请求参数
            var param = userManage.getQueryCondition(data);
            $.ajax({
                type: "POST",
                url: "/report/selectMoreTimesMealByPage",
                cache: false,  //禁用缓存
                data: param,    //传入已封装的参数
                dataType: "json",
                success: function (result) {
                    //异常判断与处理
                    if (result.errorCode) {
                        alert("查询失败");
                        return;
                    }
                    //封装返回数据
                    var returnData = {};
                    returnData.draw = data.draw;//这里直接自行返回了draw计数器,应该由后台返回
                    returnData.recordsTotal = result.total;//总记录数
                    returnData.recordsFiltered = result.total;//后台不实现过滤功能，每次查询均视作全部结果
                    returnData.data = result.pageData;
                    //调用DataTables提供的callback方法，代表数据已封装完成并传回DataTables进行渲染
                    //此时的数据需确保正确无误，异常判断应在执行此回调前自行处理完毕
                    callback(returnData);
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    alert("查询失败");
                }
            });
        },
        //绑定数据
        columns: [
            {
                data: "sno",//终端编号
                orderable: true,
            },
            {
                data: "name",//终端标识码
                orderable: true,
            },
            {
                data: "payTime",//pos号
                orderable: true,
            },
            {
                data: "imei",//pos号
                orderable: true,
            },
            {
                data: "remark",//pos号
                orderable: true,
            },
            {
                data: "attach",//pos号
                orderable: true,
            }
        ]
    })).api();//此处需调用api()方法,否则返回的是JQuery对象而不是DataTables的API对象

    //查询
    $("#btn_select").click(function () {
        _table.draw();
    });
});
var userManage = {
    getQueryCondition: function (data) {
        var param = {};
        //组装排序参数
        if (data.order && data.order.length && data.order[0]) {
            switch (data.order[0].column) {
                case 0:
                    param.orderColumn = "r.sno";//
                    break;
                case 1:
                    param.orderColumn = "r.name";//
                    break;
                case 2:
                    param.orderColumn = "r.pay_time";//
                    break;
                case 3:
                    param.orderColumn = "r.meal_name";//
                    break;
                case 4:
                    param.orderColumn = "r.canteen_name";//
                    break;
                case 5:
                    param.orderColumn = "r.times";//
                    break;
                default:
                    param.orderColumn = "r.pay_time";//
                    break;
            }
            //排序方式asc或者desc
            param.orderDir = data.order[0].dir;
        }
        param.groupName = $("#groupName").val();//查询条件
        //组装分页参数
        param.startIndex = data.start;
        param.pageSize = data.length;
        param.draw = data.draw;
        param.mealStartTime = $("#startTime").val();
        param.mealStopTime = $("#endTime").val();
        param.imei = $("#canTeenId").val();
        return param;
    }
};

/*商户-增加*/
function terminal_add(title, url, w, h) {
    layer_show(title, url, w, h);
}

/*管理员-编辑*/
function terminal_edit(title, url, id, w, h) {
    layer_show(title, url, w, h);
}

$("#btnExportMoreTimesMeal").click(function () {
    layer.msg("正在导出，请稍等", {icon: 0, time: 5000});
    var mealStartTime = $("#startTime").val();
    var mealStopTime = $("#endTime").val();
    var imei = $("#canTeenId").val();


    var url = "/report/exportMoreTimesMeal?imei=" + imei + "&mealStopTime=" + mealStopTime + "&mealStartTime=" + mealStartTime;
    window.location.href = url;
})